package result

import (
	"context"
	"html/template"
	"io"

	"gitee.com/bjf-fhe/apicat/entry/etype"
	"gitee.com/bjf-fhe/apicat/errors"
)

type GenerationData struct {
	AnalysisResult *Stat
	Labels         map[etype.EntryError]string
}

func GenerateReport(writer io.Writer, stat *Stat, ctx context.Context) error {
	var err error
	temp := template.New("generator")
	temp, err = temp.Parse(defaultTemplate)
	if err == nil {
		data := GenerationData{
			AnalysisResult: stat,
			Labels:         etype.Levels(),
		}
		err = temp.Execute(writer, &data)
		if err != nil {
			err = errors.Wrap(err, errors.ExecTemplateError)
		}
	} else {
		err = errors.Wrap(err, errors.ReadTemplateError)
	}
	return err
}
